<template>
  <dev-navbar :bgColor="'#AFD1D9'">
    <template slot="nav">
      <view class="head-portrait" @click="goCounselor">
        <image
          class="head-image"
          :src="(info && info.adviserAvatarUrl) ? info.adviserAvatarUrl : 'https://pics.leshiguang.com/vpage/2024/01/21/ea4300f8113c4b8e99aeaa16e03be0d1.png'"
        >
        </image>
        
        <view v-if="info" class="head-desc">{{ randomDesc }}</view>
        <view v-else>欢迎来到百岁园</view>
        <view @click.stop="goQR" class="qr-image">
          <image class="qr-image" src="http://static.bsyjk.cn/l/qrcode_icon.png" lazy-load="false" binderror="" bindload="" />
        </view>
        
      </view>
    </template>
  </dev-navbar>
</template>

<script>

import { parseURL } from '@/common/utils/index'

export default {
  components: {},
  props: {
    info: {
      type: Object | undefined
    }
  },
  data() {
    return {
      list: [
        '有问题随时联系我哦~',
        '记得保持健康生活'
      ]
    };
  },

  computed: {
    randomDesc() {
      var randomNumber = Math.floor(Math.random() * 2);
      return this.list[randomNumber]
    }
  },
  methods: {
    goCounselor() {
      uni.navigateTo({
        url: "/pagesMy/my/counselor/counselor",
      });
    },

    goQR() {
      wx.scanCode({
        onlyFromCamera: false,
        success: (res) => {
          console.log(res, '扫码数据----')
          console.log('条码类型：' + res.scanType);
          console.log('条码内容：' + res.result);

          //判断是自己生成的二维码还是 通过微信生成的二维码 自己生成的二维码走 result 微信的走path
          if (res.result && res.scanType !='WX_CODE') {
            this.handleQRResult(res.result);
          } else {
            uni.navigateTo({
              url: '/'+res.path
            });
          }

        },
        fail: (err) => {
          console.log(err, '失败')
          if (err.errMsg.indexOf('cancel') >= 0) {
            // 取消
          } else {
            this.$message.info('请扫描正确的二维码!')
          }
          
        }
      });
    },

    handleQRResult(result) {
      const { host, path, query, protocol, fragment } = parseURL(result);
      console.log('test', host, path, query, protocol, fragment)
      if (host == 'static.bsyjk.cn' && path.indexOf('/check') == 0) {
        this.$urouter.navigateTo({
					url: '/check/index',
					params: {
						q: encodeURIComponent(result)
					}
				});
      } else {
        this.$message.info('不支持的二维码')
      }
      
    }
  },
};
</script>
<style lang="scss" scoped>
.head-portrait {
  position: relative;
  display: flex;
  align-items: center;
  gap: 4px;
}

.head-image {
			width: 32px;
			height: 32px;
      border-radius: 16px;
		}

    .qr-image {
      width: 20px;
      height: 20px;
    }
</style>
